package com.heima.wemedia.controller.v1;

import com.heima.api.wemedia.WmNewsControllerApi;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.media.dtos.NewAuthDto;
import com.heima.model.media.dtos.WmNewsDto;
import com.heima.model.media.dtos.WmNewsPageReqDto;
import com.heima.model.media.pojos.WmNews;
import com.heima.model.media.pojos.WmNewsVo;
import com.heima.wemedia.service.WmNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Title: TODO
 * @Description: TODO
 * @author: 丁丁
 * @date: 2021/3/17
 * @version:
 */
@RestController
@RequestMapping("/api/v1/news")
public class WmNewsController implements WmNewsControllerApi {
    @Autowired
    private WmNewsService wmNewsService;

    /**
     * 分页带条件查询自媒体文章列表
     *
     * @param dto
     * @return
     */
    @Override
    @PostMapping("/list")
    public ResponseResult finAll(@RequestBody WmNewsPageReqDto dto) {
        return wmNewsService.findAll(dto);
    }

    /**
     * 提交文章
     * @param dto
     * @return
     */
    @Override
    @PostMapping("/submit")
    public ResponseResult summitNews(@RequestBody WmNewsDto dto) {
        if (dto.getStatus()== WmNews.Status.SUBMIT.getCode()){
            // 提交文章
            return wmNewsService.saveNews(dto, WmNews.Status.SUBMIT.getCode());
        } else {
            // 保存草稿
            return wmNewsService.saveNews(dto, WmNews.Status.NORMAL.getCode());
        }
    }

    /**
     * 根据id获取文章信息
     * @param id
     * @return
     */
    @Override
    @GetMapping("/one/{id}")
    public ResponseResult findWmNewsById(@PathVariable("id") Integer id) {
        return wmNewsService.findWmNewsById(id);
    }

    /**
     * 删除文章
     * @param id
     * @return
     */
    @Override
    @GetMapping("/del_news/{id}")
    public ResponseResult delNews(@PathVariable("id") Integer id) {
        return wmNewsService.delNews(id);
    }

    /**
     * 上或下架
     * @param dto
     * @return
     */
    @Override
    @PostMapping("/down_or_up")
    public ResponseResult downOrUp(@RequestBody WmNewsDto dto) {
        return wmNewsService.downOrUp(dto);
    }

    /**
     * 根据id查询自媒体文章信息
     * @param id
     * @return
     */
    @GetMapping("/findOne/{id}")
    @Override
    public WmNews findById(@PathVariable("id") Integer id) {
        return wmNewsService.findById(id);
    }

    /**
     * 修改自媒体文章信息
     *
     * @param wmNews
     * @return
     */
    @Override
    @PostMapping("/update")
    public ResponseResult updateWmnews(@RequestBody WmNews wmNews) {
        return wmNewsService.updateWmnews(wmNews);
    }

    /**
     * 查询待发布文章
     * @return
     */
    @Override
    @GetMapping("/findRelease")
    public List<Integer> findRelease() {
        return wmNewsService.findRelease();
    }

    /**
     * 查询文章列表
     * @param newAuthDto
     * @return
     */
    @Override
    @PostMapping("/findList")
    public PageResponseResult findList(@RequestBody NewAuthDto newAuthDto) {
        return wmNewsService.findListAndPage(newAuthDto);
    }

    /**
     * 查询文章详情
     *
     * @param id
     * @return
     */
    @Override
    @GetMapping("/find_news_vo/{id}")
    public WmNewsVo findWmNewsVo(@PathVariable("id") Integer id) {
        return wmNewsService.findWmNewsVo(id);
    }
}
